package person.bangbang.im.Androidgin.UI;

import java.util.List;

import person.bangbang.im.Androidgin.R;
import person.bangbang.im.Androidgin.Framework.ProtocolSpec;
import person.bangbang.im.Androidgin.Framework.StatusType;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * a adapter for protocol spinner.
 *
 */
public class ProtocolAdapter extends ArrayAdapter<ProtocolSpec> {
		private static final int ICON_DEFAULT = R.drawable.androidgin;
		private int res; // not use now
		public ProtocolAdapter(Context context, int textViewResourceId,
				ProtocolSpec[] objects) {
			super(context, textViewResourceId, objects);
			res = textViewResourceId;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ProtocolSpec s = getItem(position);
			if (1 == 0) {  // can not work why? BUG for android???
				ProtocolView v;
				if (null != convertView && convertView instanceof ProtocolView){
					v = (ProtocolView) convertView;
					v.setId(s.getId());
				} else 	{
					v = new ProtocolView(getContext(), s);
					v.setId(s.getId()); 
				}
				
				return v;
			} else  {
				 LinearLayout l;
				 
					 if (null == convertView) {
						 l = new LinearLayout(getContext());
						 LayoutInflater inflate = (LayoutInflater) (getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE));
						 inflate.inflate(R.layout.protoocl_spinner_item, l, true);
					 } else {
						 l = (LinearLayout) convertView;
					 }
					    ImageView iconView = (ImageView)l.findViewById(R.id.protocol_icon);
					    TextView idView = (TextView)l.findViewById(R.id.protocol_id);
					      
					    iconView.setImageResource((getItem(position).getIcon() != ProtocolSpec.ICON_ID_DEFAULT) ? getItem(position).getIcon() : ProtocolSpec.ICON_ID_DEFAULT);
					    idView.setText(getItem(position).getId());
					
					return l;	
			}
			
		}
		@Override
		public View getDropDownView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
			return getView(position, convertView, parent);
		}
}
